#define _CRT_SECURE_NO_WARNINGS

#include "Stack.h"

void Init(Stack* p)
{
	assert(p);
	p->a = NULL;
	p->capacity = 0;
	p->top = 0;
}

void  StackDestroy(Stack* p)
{
	assert(p);
	free(p->a);
	p->a = NULL;
	p->capacity = p->top = 0;
}

void StackPush(Stack* p, DataType x)
{
	assert(p);
	if (p->top == p->capacity)
	{
		int NewC = p->capacity == 0 ? 4 : p->capacity * 2;
		DataType* tmp = (DataType*)realloc(p->a, sizeof(DataType) * NewC);
		if (tmp == NULL)
		{
			perror("relloc error");
			return;
		}

		p->a = tmp;
		p->capacity = NewC;
	}

	p->a[p->top] = x;
	p->top++;
}
void StackPop(Stack* p)
{
	assert(p);
	assert(p->top > 0);
	p->top--;
}

DataType StackTop(Stack* p)
{
	assert(p);
	assert(p->top>0);
	return p->a[p->top-1];
}

bool StackEmpty(Stack* p)
{
	assert(p);
	return p->top == 0;
}
int StackSize(Stack* p)
{
	assert(p);
	return p->top;
}
